document.addEventListener('DOMContentLoaded', function() {
    // 底部导航切换
    const navItems = document.querySelectorAll('.nav-item');
    const pages = document.querySelectorAll('.page');
    
    navItems.forEach(item => {
        item.addEventListener('click', function() {
            const target = this.getAttribute('data-page');
            
            // 更新导航项状态
            navItems.forEach(nav => nav.classList.remove('active'));
            this.classList.add('active');
            
            // 更新页面显示
            pages.forEach(page => {
                if (page.id === target) {
                    page.classList.add('active');
                } else {
                    page.classList.remove('active');
                }
            });
        });
    });
    
    // 顶部导航标签切换
    const tabs = document.querySelectorAll('.tab');
    
    tabs.forEach(tab => {
        tab.addEventListener('click', function() {
            const parent = this.parentElement;
            const siblings = parent.querySelectorAll('.tab');
            
            siblings.forEach(sib => sib.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // 菜单页分类切换
    const categories = document.querySelectorAll('.category');
    
    categories.forEach(category => {
        category.addEventListener('click', function() {
            categories.forEach(cat => cat.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // 订单页标签切换
    const orderTabs = document.querySelectorAll('.order-tab');
    
    orderTabs.forEach(tab => {
        tab.addEventListener('click', function() {
            orderTabs.forEach(t => t.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // 购物车数量调整
    const minusBtns = document.querySelectorAll('.minus');
    const plusBtns = document.querySelectorAll('.plus');
    const quantities = document.querySelectorAll('.quantity');
    
    minusBtns.forEach((btn, index) => {
        btn.addEventListener('click', function() {
            let value = parseInt(quantities[index].textContent);
            if (value > 1) {
                quantities[index].textContent = value - 1;
            }
        });
    });
    
    plusBtns.forEach((btn, index) => {
        btn.addEventListener('click', function() {
            let value = parseInt(quantities[index].textContent);
            quantities[index].textContent = value + 1;
        });
    });
    
    // 添加到购物车按钮
    const addToCartBtns = document.querySelectorAll('.add-to-cart');
    
    addToCartBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            // 模拟添加到购物车的动画效果
            this.classList.add('added');
            setTimeout(() => {
                this.classList.remove('added');
            }, 500);
        });
    });
    
    // 清空购物车按钮
    const clearCartBtn = document.querySelector('.clear-cart');
    const cartItems = document.querySelector('.cart-items');
    
    if (clearCartBtn && cartItems) {
        clearCartBtn.addEventListener('click', function() {
            if (confirm('确定要清空购物车吗？')) {
                // 模拟清空购物车
                cartItems.innerHTML = '<div class="empty-cart">购物车为空</div>';
            }
        });
    }
    
    // 结账按钮
    const checkoutBtn = document.querySelector('.checkout-btn');
    
    if (checkoutBtn) {
        checkoutBtn.addEventListener('click', function() {
            alert('订单已提交！');
            // 模拟跳转到订单页
            document.querySelector('[data-page="order"]').click();
        });
    }
    
    // 事件触发按钮
    const eventTriggers = document.querySelectorAll('.event-trigger');
    
    eventTriggers.forEach(trigger => {
        trigger.addEventListener('click', function() {
            alert('事件已触发，积分已增加！');
        });
    });
    
    // 初始化：默认显示首页
    document.querySelector('.nav-item[data-page="home"]').classList.add('active');
    document.querySelector('#home').classList.add('active');
});